package me.zixing.memorandum.ui.component;

import me.zixing.memorandum.domain.DayDO;
import me.zixing.memorandum.domain.DayTask;
import me.zixing.memorandum.utils.AppConstant;
import me.zixing.memorandum.utils.TaskPanelUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TheDayPanel extends JPanel {
    private DayDO dayDO;
    private JViewport jViewport;
    private JLabel label;
    private JLabel lunarLabel;
    private boolean selected;
    public static boolean inited = false;//是否初始化完成
    private List<DayTask> dayTasks;

    public JViewport getjViewport() {
        return jViewport;
    }

    public void setjViewport(JViewport jViewport) {
        this.jViewport = jViewport;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public DayDO getDayDO() {
        return dayDO;
    }

    public void setDayDO(DayDO dayDO) {
        this.dayDO = dayDO;
    }

    private MouseListener mouseListener = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            //requestFocus();
            if(e.getClickCount() == 2){
                FormPanel panel = new FormPanel(TheDayPanel.this);
            }else{
                Component[] components = getParent().getComponents();

                for (Component component:components) {
                    if(component instanceof TheDayPanel){
                        TheDayPanel theDayPanel = TheDayPanel.class.cast(component);
                        theDayPanel.label.setForeground(null);
                        theDayPanel.lunarLabel.setForeground(null);
                        theDayPanel.label.setFont(AppConstant.SOFT_ITALIC_15);
                        theDayPanel.setSelected(false);
                        if(theDayPanel.getDayDO().getDayOfMonth() == Integer.parseInt(new SimpleDateFormat("dd").format(new Date()))){
                            theDayPanel.label.setFont(AppConstant.SOFT_BOLD_15);
                            theDayPanel.label.setForeground(AppConstant.MAIN_COLOR);
                            theDayPanel.lunarLabel.setForeground(AppConstant.MAIN_COLOR);
                        }
                    }
                }

                TheDayPanel.this.label.setForeground(AppConstant.MAIN_COLOR);
                TheDayPanel.this.lunarLabel.setForeground(AppConstant.MAIN_COLOR);
                TheDayPanel.this.label.setFont(AppConstant.SOFT_BOLD_15);
                TheDayPanel.this.setSelected(true);

                TaskPanelUtils.refreshTasksPanel(jViewport,dayDO);
            }

        }

    };

    public TheDayPanel(DayDO dayDO,JViewport jViewport) {
        this.dayDO = dayDO;
        this.jViewport  = jViewport;
        this.dayTasks = dayDO.getDayTasks();
        this.setLayout(null);
        label = new JLabel(""+dayDO.getDayOfMonth());
        lunarLabel = new JLabel(""+dayDO.getDayForLunar());
        label.setFont(AppConstant.SOFT_ITALIC_15);
        if(dayDO.getDayOfMonth() < 10){
            label.setBounds(25,5,20,20);
        }else{
            label.setBounds(20,5,20,20);
        }
        lunarLabel.setBounds(18,20,50,20);
        this.add(lunarLabel);
        this.add(label);
        this.addMouseListener(mouseListener);
    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        try{
            if(dayDO.getDayOfMonth() == Integer.parseInt(new SimpleDateFormat("dd").format(new Date())) && !inited){
                this.label.setForeground(AppConstant.MAIN_COLOR);
                this.lunarLabel.setForeground(AppConstant.MAIN_COLOR);
                this.label.setFont(AppConstant.SOFT_BOLD_15);
                TaskPanelUtils.refreshTasksPanel(jViewport,dayDO);
                inited = true;
                setSelected(true);
            }
            if(isSelected()){
                g.drawImage(ImageIO.read(getClass().getClassLoader().getResourceAsStream("select.png")),5,0,null);
            }
            if(dayDO.getDayTasks().size() != 0){
                g.drawImage(ImageIO.read(getClass().getClassLoader().getResourceAsStream("point.png")),20,30,20,20,null);
            }
            repaint();


        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
